There is always some confusion about why we need singleton class instead of using static. I can explain it with few program samples. Lets create a singleton class first.
package {
   import flash.events.*;
   public class Singleton extends EventDispatcher {
    public static var singletonObj:Singleton;
    public static function getInstance():Singleton {
       if (singletonObj==null) {
            singletonObj = new Singleton();
       }
       return singletonObj;
     }
    public function callTrigger() {
     dispatchEvent(new Event("onTrigger"));
    }
   }
}


Now lets create other two classes which are going to use this singleton class. Lets say FirstClass.as and SecondClass.as.

FirstClass.as
package {
     import flash.events.*;
     public class FirstClass {
         var singletonObj:Singleton;
         var secClass:SecondClass;
         public function FirstClass() {
             secClass = new SecondClass();
             singletonObj=Singleton.getInstance();
             singletonObj.addEventListener("onTrigger", testEvent);
             singletonObj.callTrigger();
         }
         private function testEvent(evt:Event) {
             trace("FirstClass");
         }
    }
}

SecondClass.as

package {
    import flash.events.*;
    public class SecondClass{
        var singletonObj:Object = new Object();
        public function SecondClass() {
            singletonObj=Singleton.getInstance();
            singletonObj.addEventListener("onTrigger", testEvent);
        }
        private function testEvent(evt:Event) {
            trace("SecondClass");
        }
    }
}

Lets consider we have created an object for FirstClass  somewhere. Inside the constructor function of FirstClass, when callTrigger() function is fired, the singleton class will dispatch the same event for both FirstClass and SecondClass as they are sharing the same object of singleton class. So, by using the singleton we are controlling the two classes with only one central/common object.

singleton.zip
File Size: 8 kb
File Type: zip
Download File

 
OOPs: Object Oriented Programming
OOP is  Organizing a program around with its Data and set the well defined interface to the Data

What are the OOPs Concepts?
OOPs use the three basic components. Class , Objects and Methods. Additionally Inheritance, polymorphism, Abstraction, Event Handling and Encapsulation are the significant concepts of OOPs.

Types of Classes (AS3)

ActionScript 3.0 supports the following class attributes:

  • dynamic (allows properties to be added to instance at run time)
  • final (cannot be extended by another class)
  • internal (visible inside the current package)
  • public (visible everywhere)
Class Property Attributes

  • internal (visible inside the same package)
  • private (visible in the same class)
  • protected (visible in the same class and derived classes)
  • public (visible everywhere
  • static (specifies that a property belongs to the class, not to instances of the class)
Encapsulation

It is an ability to hide and protect the data. For example, getters and setters in a class. We don’t need to know about the properties we get from the getters, but we should know what should be sent to the setters. So these getters and setters are used to make your class properties private and also accessible by the other classes.

Inheritance

Inheritance is a form of a code reuse which allows programmers to develop new classes that are based on the existing classes. Advantage of inheritance is the way it allows you to reuse the code from the base class and leave the existing code unmodified.

Interfaces

Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. Implementing an interface allows a class to become more formal about the behavior it promises to provide. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. Lets see a simple code later in another post.

By using interfaces, you can maintain the consistency through-out the application. It will also make you (or anyone else editing/implementing your interfaces) to not leave any methods that is necessary to a class. It is more useful in cases where you can use more classes.

See at the end of this thread to download a sample interface classes in AS3.

Function Overloading

Function overloading is one attribute of Polymorphism in OOP. Writing two methods with the same name and different parameters is called Function Overloading. Function overloading is available in AS2 but AS3 do not support. See the code example.

function MyFun() {
   trace("Function");
}
function MyFun(msg:String) {
   trace(msg);
}

This sample will work with AS2 compiler but not with AS3 compiler. To overload a function in AS3, we can use the “*” as datatype for the parameter which will get any parameter type with the same function name and we can add our codes to behave according to the type of arguments.

function doSomething(obj:*) : int
{
  if (obj is int)
  // do int stuff
  else if (obj is String)
  // do string stuff
  else
  // what type did you give me?
}

Singleton Class:

Singleton class restrict the limit of instantiation of the objects. In other words, its a class which have only one object. For example, configuration data is stored in one singletone class, and objects for this class have initialized in many classes. In this case, data in configuration is important for all the classes, so everything is managed by a single object or central object. Any change in configuration can be easily notified for all linked classes.

interfaces_oop.zip
File Size: 8 kb
File Type: zip
Download File

 
Action Script:

  • It was originally developed by Macromedia Inc.
  • It is a specific group of ECMAScript. (Its a scripting language standardized by Ecma international which is a non-profit standards organization for Information and Communication Systems)
  • It has the same syntax and semantics of well known script JavaScript.
  • It was initially designed for 2D animation with simple vector graphics.
  • Later version are designed to focus on wide range of Rich Internet Application (RIA), Games and Streaming audio and videos.
  • Now, we have three version of AS, called AS1 , AS2 and AS3 (the latest one).
  • Flash version 1 to 6 supports AS, Flash 7 and 8 supports AS and AS2, all other versions supports AS2 and AS3.
  • AS2 is based on ECMAScript 4 which runs on AVM1 (ActionScript Virtual Machine1) whereas AS3 was introduced with some major improvement of performance in AVM1 (called as AVM2).
  • AS3 version has much improvement in performance with Flash player using Just In Time (JIT) compiler.
  • AS3 version supports Binary sockets, E4x XML parsing, Full-Screen Mode and inclusion of Regular Expression. Binary Sockets are used to work with  remote server connectivity.